#include "WijiDialog.h"
#include <QWebView>
#include <QSettings>
#include "WijiUi.h"
WijiDialog::WijiDialog(QWidget *parent) : QMainWindow(parent) {
    webView = new QWebView(this);
    connect(webView->page(), SIGNAL(windowCloseRequested()),this, SLOT(windowCloseRequested()));
    connect(webView->page(),SIGNAL(linkClicked(QUrl)),this,SLOT(linkClicked(QUrl)));
}
void WijiDialog::linkClicked(QUrl url) {
    WijiSettings* ws = WijiSettings::getSelf();
    QScriptValue global = scripter.engine.globalObject();
    if (!global.property(QLatin1String("REQUEST")).isValid()) {
        global.setProperty(QLatin1String("REQUEST"), url.toString());
    } else {
        global.setProperty(QLatin1String("REQUEST"), url.toString());
    }
    QScriptValue result = scripter.runQtScript(QString(ws->getValue("system_scripts").toString()) + "/dialogrouter.qs"); //i.e. we let the router handle everything...easier that way.
    webView->load(QUrl(result.toString()));
}
void WijiDialog::setSize(int width, int height) {
    QSize size(width,height);
    resize(size);
    webView->resize(size);
}
void WijiDialog::display() {

    setCentralWidget(webView);
    show();
}
void WijiDialog::setHtml(QString html) {
    WijiSettings* ws = WijiSettings::getSelf();
    webView->setHtml(html,QUrl(ws->getValue("base_url").toString()));
}
void WijiDialog::setUrl(QString url) {
    webView->load(QUrl(url));
}
void WijiDialog::windowCloseRequested() {
    close();
}
WijiDialog::~WijiDialog()
{

}
